<template>
  <!-- 动态数据源配置 -->
  <div>
    <TableHead v-model="data.head" ref="TableHead" />
    <el-form-item label="数据类型">
      <el-radio-group v-model="data.type">
        <el-radio-button label="static">静态数据</el-radio-button>
        <el-radio-button label="dynamic">动态数据</el-radio-button>
      </el-radio-group>
    </el-form-item>
    <el-form-item v-if="data.head !== undefined" label="表头数据">
      <el-button type="primary" plain size="small" @click="$refs.TableHead.open()">编辑数据</el-button>
    </el-form-item>
    <template v-if="data.type=='static'">
      <!-- 树形结构增删改 -->
      <FGTreeNode v-model="data.value" v-if="data.valueType=='tree'" />
      <!-- 列表结构增删改 -->
      <FGOptions :options.sync="data.value" v-else-if="data.valueType=='list'" />
      <el-form-item label="数据内容" v-else>
        <el-button type="primary" plain @click="$refs.JsonDialog.open()">编辑数据</el-button>
        <JsonDialog ref="JsonDialog" v-model="data.value" />
      </el-form-item>
    </template>

    <el-form-item label="数据内容" v-if="data.type=='dynamic'">
      <el-button plain type="primary" @click="$refs.DataDialog.open()">编辑数据</el-button>
      <DataDialog ref="DataDialog" v-model="data" />
      <el-button type="primary" size="small" @click="handleRefresh">刷新</el-button>
    </el-form-item>
    <template v-if="data.props">
      <el-form-item label="标签键名" v-if="data.props.label !== undefined">
        <el-input v-model="data.props.label" placeholder="请输入标签键名" />
      </el-form-item>
      <el-form-item label="值键名" v-if="data.props.value !== undefined">
        <el-input v-model="data.props.value" placeholder="请输入值键名" />
      </el-form-item>
      <el-form-item label="子级键名" v-if="data.props.children !== undefined">
        <el-input v-model="data.props.children" placeholder="请输入子级键名" />
      </el-form-item>
      <el-form-item label="叶子节点键名" v-if="data.props.isLeaf !== undefined">
        <el-input v-model="data.props.isLeaf" placeholder="请输入子级键名" />
      </el-form-item>
      <el-form-item label="图标键名" v-if="data.props.icon !== undefined">
        <el-input v-model="data.props.icon" placeholder="请输入子级键名" />
      </el-form-item>
      <el-form-item label="类键名" v-if="data.props.className !== undefined">
        <el-input v-model="data.props.className" placeholder="请输入子级键名" />
      </el-form-item>
      <template v-if="data.type=='dynamic'">
        <el-form label-position="top" size="small" v-if="isPage">
          <el-form-item label="接口响应数据中总条目数的字段名称(分页)" v-if="data.props.total !== undefined">
            <el-input v-model="data.props.total" placeholder />
          </el-form-item>
          <el-form-item label="接口响应数据中列表数据的字段名称(分页)" v-if="data.props.records !== undefined">
            <el-input v-model="data.props.records" placeholder />
          </el-form-item>
          <el-form-item label="请求接口时页码的参数名称(分页)" v-if="data.props.current !== undefined">
            <el-input v-model="data.props.current" placeholder />
          </el-form-item>
          <el-form-item label="请求接口时条目数的参数名称(分页)" v-if="data.props.size !== undefined">
            <el-input v-model="data.props.size" placeholder />
          </el-form-item>
        </el-form>
      </template>

      <template v-if="data.props &&data.props.request">
        <el-divider>请求数据</el-divider>
        <el-form-item v-for="(value, key) in data.props.request" :key="key" :label="key + ' 键名'">
          <el-input v-model="data.props.request[key]" />
        </el-form-item>
      </template>
      <template v-if="data.props &&data.props.response">
        <el-divider>响应数据</el-divider>
        <el-form-item v-for="(value, key) in data.props.response" :key="key" :label="key + ' 键名'">
          <el-input v-model="data.props.response[key]" />
        </el-form-item>
      </template>
    </template>
  </div>
</template>

<script>
import { deepClone } from "../utils";
import DataDialog from "./DataDialog";
import FGTreeNode from "../huComponents/FGTreeNode.vue";
import FGOptions from "../huComponents/FGOptions.vue";
import JsonDialog from "@/components/editor/JsonDialog";
import TableHead from "./TableHead";
import store from "@/store";
export default {
  name: "DataConfig",
  components: {
    FGTreeNode,
    DataDialog,
    JsonDialog,
    TableHead,
    FGOptions,
  },
  props: {
    value: {
      type: Object,
      default: () => {},
    },
    // 是否分页
    isPage: {
      type: Boolean,
      default: false,
    },
    // 数据类型，若为空则可以手动选择，若有数据则固定位只能静态/动态
    dateType: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      data: {},
      activeNames: [],
    };
  },
  watch: {
    data: {
      immediate: true,
      deep: true,
      handler(newValue) {
        if (newValue) {
          this.$emit("input", newValue);
        }
      },
    },
    value: {
      immediate: true,
      deep: true,
      handler(newValue) {
        if (JSON.stringify(newValue) !== JSON.stringify(this.data)) {
          this.data = deepClone(this.value);
        }
      },
    },
  },
  methods: {
    // 刷新
    handleRefresh() {
      store.commit("SET_REFRESH");
    },
  },
};
</script>

<style lang="scss" >
</style>